package com.suitsoft.bdms.dao.imp;


import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.stereotype.Repository;

import com.suitsoft.bdms.commos.UserHolder;

/**
 *
 *@author Suine
 */
@Repository
public class ImpDao {
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private DataSource dataSource;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public List<Map<String,Object>>getAllSync(){
		String sql = "SELECT * FROM BD_POIT_LOGIN";
		return jdbcTemplate.queryForList(sql);
	}
	
	public Map<String,Object> get(Long id){
		String sql = " SELECT * FROM BD_POIT_LOGIN where id = " + id;
		return jdbcTemplate.queryForMap(sql);
	}
	
	SimpleJdbcCall proc = null;
	MapSqlParameterSource in;
	
	public void student(){
		proc = new SimpleJdbcCall(dataSource).withProcedureName("PRO_IMP_STUDENT");
		Long userId = UserHolder.getLoginUser().getId();
		
		in = new MapSqlParameterSource();
		in.addValue("v_opuser", userId);
		proc.execute(in);
	}
	
	public void teacher(){
		proc = new SimpleJdbcCall(dataSource).withProcedureName("PRO_IMP_TEACHER");
		Long userId = UserHolder.getLoginUser().getId();
		
		in = new MapSqlParameterSource();
		in.addValue("v_opuser", userId);
		proc.execute(in);
	}
	
	public void department(){
		proc = new SimpleJdbcCall(dataSource).withProcedureName("PRO_IMP_BASE_DEPARTMENT");
		Long userId = UserHolder.getLoginUser().getId();
		
		in = new MapSqlParameterSource();
		in.addValue("v_opuser", userId);
		proc.execute(in);
	}
	
	public void staffroom(){
		proc = new SimpleJdbcCall(dataSource).withProcedureName("PRO_IMP_STAFFROOM");
		Long userId = UserHolder.getLoginUser().getId();
		
		in = new MapSqlParameterSource();
		in.addValue("v_opuser", userId);
		proc.execute(in);
	}
	
	public void baseClass(){
		proc = new SimpleJdbcCall(dataSource).withProcedureName("PRO_IMP_CLASS");
		Long userId = UserHolder.getLoginUser().getId();
		
		in = new MapSqlParameterSource();
		in.addValue("v_opuser", userId);
		proc.execute(in);
	}
	
	public void major(){
		proc = new SimpleJdbcCall(dataSource).withProcedureName("PRO_IMP_MAJOR");
		Long userId = UserHolder.getLoginUser().getId();
		
		in = new MapSqlParameterSource();
		in.addValue("v_opuser", userId);
		proc.execute(in);
	}
	
	public void studentTask(String xn,String xq){
		proc = new SimpleJdbcCall(dataSource).withProcedureName("PRO_IMP_XK_RESULT");
		Long userId = UserHolder.getLoginUser().getId();
		
		in = new MapSqlParameterSource();
		in.addValue("v_opuser", userId);
		in.addValue("xq", xq);
		in.addValue("xn", xn);
		proc.execute(in);
	}
	
	public void teacherTask(){
		proc = new SimpleJdbcCall(dataSource).withProcedureName("PRO_IMP_TEACHER_COURSE_R");
		Long userId = UserHolder.getLoginUser().getId();
		
		in = new MapSqlParameterSource();
		in.addValue("v_opuser", userId);
		proc.execute(in);
	}
	
	public void post(){
		proc = new SimpleJdbcCall(dataSource).withProcedureName("PRO_IMP_POST");
		Long userId = UserHolder.getLoginUser().getId();
		
		in = new MapSqlParameterSource();
		in.addValue("v_opuser", userId);
		proc.execute(in);
	}
	
	public void departmentMajor(){
		proc = new SimpleJdbcCall(dataSource).withProcedureName("PRO_IMP_D_M_R");
		Long userId = UserHolder.getLoginUser().getId();
		
		in = new MapSqlParameterSource();
		in.addValue("v_opuser", userId);
		proc.execute(in);
	}
	
	public void syncUser(String viewName){
		proc = new SimpleJdbcCall(dataSource).withProcedureName("PRO_IMP_SUB_SYSTEM_USER_DATA");
		Long userId = null;
		if(UserHolder.getLoginUser() == null){
			userId = -1L;
		}else{
			userId = UserHolder.getLoginUser().getId();
		}
		in = new MapSqlParameterSource();
		in.addValue("v_opuser", userId);
		in.addValue("v_data_view", viewName);
		proc.execute(in);
	}
	
	public void testTimer(String viewName){
		proc = new SimpleJdbcCall(dataSource).withProcedureName("PRO_TEST_TIMER");
		in = new MapSqlParameterSource();
		in.addValue("view_name", viewName);
		Map<String,Object> m = proc.execute(in);
		System.out.println(m);
	}
}

